package Request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/requestDemo1")
public class requestDemo1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求方式
        String method=req.getMethod();
        System.out.println(method);//GET

        //2.(重要)获取虚拟目录
        String contextPath = req.getContextPath();
        System.out.println(contextPath);//  /Servlet3_0

        //3.获取Servlet路径
        String servletPath = req.getServletPath();
        System.out.println(servletPath);//  /requestDemo1

        //4.获取get方式请求参数
        String queryString = req.getQueryString();
        System.out.println(queryString);//null

        //5.(重要)获取请求URI
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);//  /Servlet3_0/requestDemo1
        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURL);//http://localhost:8080/Servlet3_0/requestDemo1

        //6.获取协议及版本
        String protocol = req.getProtocol();
        System.out.println(protocol);// HTTP/1.1

        //7.获取客户机的IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);//0:0:0:0:0:0:0:1
    }
}
